package org.jeecg.modules.bj.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.activiti.engine.HistoryService;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.bj.entity.*;
import org.jeecg.modules.bj.service.IUpdateBussinessService;
import org.jeecg.modules.bj.service.IYgdabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UpdateBussinessServiceImpl implements IUpdateBussinessService {
    @Autowired
    private HistoryService historyService;
    @Autowired
    private ActBusinessServiceImpl actBusinessService;
    @Autowired
    BjIdcardfkServiceImpl bjIdcardfkService;
    @Autowired
    BjYgruzhiServiceImpl bjYgruzhiService;
    @Autowired
    BjQuitServiceImpl bjQuitService;
    @Autowired
    BjGwbdServiceImpl bjGwbdService;
    @Autowired
    OpBjDbServiceImpl opBjDbService;


    @Override
    public void updateBussiness(String proceInstId) {
        System.out.println(proceInstId);
        int size=historyService.createHistoricProcessInstanceQuery().finished().processInstanceId(proceInstId).list().size();
        if(size==1){
            ActBusiness actBusiness=actBusinessService.findByProcInstId(proceInstId).get(0);
            if(actBusiness.getTitle().equals("员工入职申请")){
                String tableId=actBusiness.getTableId();
                BjIdcardfk bjIdcardfk= bjIdcardfkService.getById(tableId);
                String ruizhijson=bjIdcardfk.getRuzhiJson();
                String[] ids=ruizhijson.split(",");
                for(String ruzhiid:ids){
                    BjYgruzhi bjYgruzhi=bjYgruzhiService.getById(ruzhiid);
                    String ygid=bjYgruzhi.getYgid();
                    opBjDbService.updateYgdab(ygid,1);
                }
            }

            if(actBusiness.getTitle().equals("员工离职申请")){
                String tableId=actBusiness.getTableId();
                BjIdcardfk bjIdcardfk= bjIdcardfkService.getById(tableId);
                String lizhijson=bjIdcardfk.getLizhiJson();
                String[] ids=lizhijson.split(",");
                for(String lizhiid:ids){
                    BjQuit bjQuit= bjQuitService.getById(lizhiid);
                    String ygid=bjQuit.getYgid();
                    opBjDbService.updateYgdab(ygid,0);
                }
            }

            if(actBusiness.getTitle().equals("员工调动变动申请")){
                String tableId=actBusiness.getTableId();
                BjIdcardfk bjIdcardfk= bjIdcardfkService.getById(tableId);
                String biandongjson=bjIdcardfk.getBiandongJson();
                String[] ids=biandongjson.split(",");
                for(String biandongid:ids){
                    BjGwbd bjGwbd= bjGwbdService.getById(biandongid);
                    String ygid=bjGwbd.getYgid();
                    opBjDbService.updateYgdabForGwbd(ygid,bjGwbd);
                    bjGwbdService.updateById(bjGwbd);
                }
            }
        }

    }

}
